﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using System.Collections;
using Components;
using UI.Lib;

namespace UI.Sysset
{
    public partial class FrmUserEdit : BaseEdit
    {
        public FrmUserEdit(string action, string text, string key)
            : base(action)
        {
            InitializeComponent();

            this.Text = text;
            this.key = key;
            //回写的方法
            this.SaveCallFuntion = "Sys0a00Manager$SaveData"; //保存的方法
            if ("A" == this.Action)
            {
                IsAfterSaveCloseWin = false;
            }
            else
            {
                LoadData();
            }

            this.Size = base.MidWindow;
        }

        protected override bool Check()
        {
            if ("A" == this.Action)
            {
                Hashtable hs = new Hashtable();
                hs["usercode"] = this.tbSummary.GetValue("usercode");
                bool reflt = UI.Lib.Factory.CommonHelper.Entity.DataInData("Sys0a00", hs);
                if (reflt)
                {
                    MessageBox.Show("当前用户编码已经存在！", "提示");
                    this.tbSummary.Focus("usercode");
                    return false;
                }

                return true;
            }

            return true;
        }

        //加载修改数据
        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.usercode ='{0}' ", this.key));
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("empcode" == name) //选择员工工号，需要用公司ID进行过滤
            {
                string comcode = Validation.ToString(this.tbSummary.GetValue("comcode"));
                where = (string.IsNullOrEmpty(comcode)) ? "" : string.Format(" EXISTS(SELECT 1 FROM Psn1a00 a WHERE a.depcode = depno AND a.comcode ='{0}' )", comcode);
            }
            else if ("rolecode" == name)
            {
                if (!Context.IsSuperUser)
                {
                    where = string.Format(" (comcode = '{0}' OR comcode IS NULL) ", Context.User.Comcode);
                }
            }
            return true;
        }

        protected override void AfterSystemChangeControlState()
        {
            if ("A" != this.Action)
            {
                this.tbSummary.Show("rolecode", false);
                this.tbSummary.Show("rolename", false);
            }
            else
            {
                this.tbSummary.SetValue("comcode", Context.Comocde);
                this.tbSummary.SetValue("comname", Context.ComoName);
            }

            //如果当期用户是超级管理员，可以编辑公司
            if (!Context.IsSuperUser)
            {
                this.tbSummary.Enable("comcode", false);
                this.tbSummary.Enable("supermk", false);
            }
        }
    }
}

